﻿#region Copyright (c) 2013-04, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using NUnit.Framework;


namespace Amarok.Agents
{
	public partial class Test_Agent
	{
		[Test]
		public void Test_Subscribe_Callback()
		{
			var environment = new MyAgentEnvironment();
			var options = new MyAgentOptions();
			var agent = new MyAgent(environment, options);
			var agentInterface = (IAgent)agent;

			Int32 callbackCalled = 0;
			Action<Message> callback = msg =>
				callbackCalled++;

			agentInterface.Subscribe(
				typeof(TestMessage),
				callback);

			agent.TestPublish(new TestMessage());

			Helper.WaitForCondition(5000, 10, () => callbackCalled == 1);
			Assert.That(callbackCalled, Is.EqualTo(1));
		}

		[Test]
		public void Test_Subscribe_Message_Agent_Filter()
		{
			var environment = new MyAgentEnvironment();
			var options = new MyAgentOptions();
			var agent = new MyAgent(environment, options);
			var subscriber = new MySubscriberAgent(environment, options);

			var subscription = agent.TestSubscribe(
				typeof(TestMessage),
				subscriber,
				null);

			agent.TestPublish(new TestMessage());

			Helper.WaitForCondition(5000, 10, () => subscriber.MessageHandlerCalled == 1);
			Assert.That(subscriber.MessageHandlerCalled, Is.EqualTo(1));
			Assert.That(subscription, Is.Not.Null);
		}

		[Test]
		public void Test_Subscribe_TMessage_Agent_Filter()
		{
			var environment = new MyAgentEnvironment();
			var options = new MyAgentOptions();
			var agent = new MyAgent(environment, options);
			var subscriber = new MySubscriberAgent(environment, options);

			var subscription = agent.TestSubscribe<TestMessage>(
				subscriber,
				null);

			agent.TestPublish(new TestMessage());

			Helper.WaitForCondition(5000, 10, () => subscriber.MessageHandlerCalled == 1);
			Assert.That(subscriber.MessageHandlerCalled, Is.EqualTo(1));
			Assert.That(subscription, Is.Not.Null);
		}

		[Test]
		public void Test_Subscribe_Message_Callback_Filter()
		{
			var environment = new MyAgentEnvironment();
			var options = new MyAgentOptions();
			var agent = new MyAgent(environment, options);

			Int32 callbackCalled = 0;
			Action<Message> callback = msg =>
				callbackCalled++;

			var subscription = agent.TestSubscribe(
				typeof(TestMessage),
				callback,
				null);

			agent.TestPublish(new TestMessage());

			Helper.WaitForCondition(5000, 10, () => callbackCalled == 1);
			Assert.That(callbackCalled, Is.EqualTo(1));
			Assert.That(subscription, Is.Not.Null);
		}

		[Test]
		public void Test_Subscribe_TMessage_Callback_Filter()
		{
			var environment = new MyAgentEnvironment();
			var options = new MyAgentOptions();
			var agent = new MyAgent(environment, options);

			Int32 callbackCalled = 0;
			Action<Message> callback = msg =>
				callbackCalled++;

			var subscription = agent.TestSubscribe<TestMessage>(
				callback,
				null);

			agent.TestPublish(new TestMessage());

			Helper.WaitForCondition(5000, 10, () => callbackCalled == 1);
			Assert.That(callbackCalled, Is.EqualTo(1));
			Assert.That(subscription, Is.Not.Null);
		}

	}
}

#endif
